﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Gui.Scan;
using Gui.Service;
using Gui.Util;

namespace Gui
{
    /// <summary>
    /// Window.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {

        
        public LoginWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Scanner.AddValidCodeHandler(onPersonCodeScan);
            Scanner.AddInValidCodeHandler(invalidCodeHandler);
            //FullScreen();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Scanner.RemoveValidCodeHandler(onPersonCodeScan);
            Scanner.AddInValidCodeHandler(invalidCodeHandler);
        }

        void invalidCodeHandler(string invalidCode)
        {
            Dispatcher.Invoke(() => {
                InfoText.Text = "非法编码："+invalidCode;
            });
        }

        void onPersonCodeScan(string peopleCode)
        {
            Dispatcher.Invoke(() =>
            {
                try
                {
                    //todo 检查人员码有效性
                    if (!CodeChecker.IsPerson(peopleCode))
                    {
                        InfoText.Text = "人员编码格式非法";
                        return;
                    }
                    InfoText.Text = "正在登录";
                    ProgressBar.Visibility = Visibility.Visible;
                    //await Api.Login(peopleCode);
                    MainWindow mainWindow = new MainWindow();
                    mainWindow.Show();
                    Close();
                }
                catch (Exception e)
                {
                    InfoText.Text = e.Message;
                    ProgressBar.Visibility = Visibility.Hidden;
                }
            });
        }


        private void FullScreen()
        {
            WindowState = WindowState.Normal;//还原窗口（非最小化和最大化）
            WindowStyle = WindowStyle.None; //仅工作区可见，不显示标题栏和边框
            ResizeMode = ResizeMode.NoResize;//不显示最大化和最小化按钮
            Topmost = true;    //窗口在最前
            Left = 0.0;
            Top = 0.0;
            Width = SystemParameters.PrimaryScreenWidth;
            Height = SystemParameters.PrimaryScreenHeight;
        }
    }

    class LoginViewModel:NotifyBase
    {
        public bool IsError
        {
            get
            {
                return _isError;
            }
            set
            {
                _isError = value;
                RaisePropertyChanged("IsError");
            }
        }

        public string ErrMsg
        {
            get
            {
                return _errMsg;
            }
            set
            {
                _errMsg = value;
                RaisePropertyChanged("ErrMsg");
            }
        }
        


        private bool _isError;
        private string _errMsg;
    }

}
